package com.hx.zhg.util;

import com.hx.zhg.constant.CommonConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 字符串工具类，对 apache.commons.lang3.StringUtils的补充
 */
public class StringUtilExt {

    /**
     * 获取字符串前N个字节
     * 输入“我ABC汉DEF”，6，应该输出为“我ABC”而不是“我ABC+汉的半个”。
     */
    public static String getSubString(String content, int size, String encoding)
            throws Exception {
        if (encoding == null || encoding.equals("")) {
            throw new IllegalArgumentException("字符编码不能为空");
        }
        if (content == null) {
            throw new IllegalArgumentException("字符串内容不能为空");
        }
        if (size < 0) {
            throw new IllegalArgumentException("获取的字节数不能小于0");
        }
        if (content.getBytes().length < size) {
            size = content.getBytes().length;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int index = 0; // 字符串的字符位置
        int count = 0; // 目前的字节数
        while (count < size) {
            // 当前的字节数目
            count += content.substring(index, index + 1).getBytes(encoding).length;
            if (count <= size) {
                out.write(content.substring(index, index + 1).getBytes(encoding));
            }
            index++;
        }
        String result = new String(out.toByteArray(), encoding);
        out.close();
        return result;
    }

    /**
     * 字符串转换编码
     *
     * @param str
     * @param origCharset 原始编码
     * @param targCharset 目标编码
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String convertStrCharset(String str, String origCharset, String targCharset)
            throws UnsupportedEncodingException {
        if (StringUtils.isEmpty(str)) {
            return "";
        }
        return new String(str.getBytes(origCharset), targCharset);
    }

    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";

        for (int n = 0; n < b.length; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
        }
        return hs.toUpperCase();
    }

    public static int compareAppVersion(String appVersionBefore, String appVersionAfter) {
        Assert.notNull(appVersionBefore);
        Assert.notNull(appVersionAfter);
        int len;
        String[] beforeArray = appVersionBefore.split("[.]");
        String[] afterArray = appVersionAfter.split("[.]");
        int beforeLen = beforeArray.length;
        int afterLen = afterArray.length;
        len = beforeLen > afterLen ? afterLen : beforeLen;
        for (int i = 0; i < len; i++) {
            int beforeNum = Integer.parseInt(beforeArray[i]);
            int afterNum = Integer.parseInt(afterArray[i]);
            if (beforeNum > afterNum) {
                return 1;
            }
            if (beforeNum < afterNum) {
                return -1;
            }
        }
        return beforeLen == afterLen ? 0 : beforeLen > afterLen ? 1 : -1;
    }


    public static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0)
            throw new IllegalArgumentException("长度不是偶数");
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }

    public static boolean notBlank(String str) {
        return null != str && "" != str && "" != str.trim();
    }

    public static boolean notBlank(Object strobj) {
        return null != strobj && "" != strobj.toString() && "" != strobj.toString().trim();
    }

    public static boolean isBlank(String str) {
        return !notBlank(str);
    }

    public static boolean isBlank(Object strobj) {
        return !notBlank(strobj);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String test = "中12国234";
        try {
            System.out.println(getSubString(test, 4, "GBK"));
            System.out.println(getSubString(test, 7, "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 数字字符串转成数字list
     * 使用都好分割的,例如:1,2,3,4,5,6,7,8,
     *
     * @param numStr
     * @return
     */
    public static List<Integer> numStrToNumList(String numStr) {
        List<Integer> result = null;
        try {
            if (StringUtils.isNotBlank(numStr)) {
                result = new ArrayList<Integer>();
                String[] split = numStr.split("[,]");
                for (int i = 0; i < split.length; i++) {
                    int num = Integer.parseInt(split[i]);
                    result.add(num);
                }
            }
        } catch (Exception e) {
        }
        return result;
    }

    /**
     * string 转成 list
     *
     * @param str
     * @param separate
     * @return
     * @throws Exception
     */
    public static List<String> strToList(String str, String separate) throws Exception {
        Assert.notNull(str);
        List<String> list = new ArrayList();
        if (StringUtils.isBlank(separate)) {
            separate = CommonConstant.SPLIT_COMMA;
        }
        String[] strArray = str.split(separate);
        for (String s : strArray) {
            try {
                list.add(s);
            } catch (Exception e) {
            }
        }
        return list;
    }

}
